//
//  MNDScanInviteViewController.m
//  minidai
//
//  Created by Vitas on 2018/9/1.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDScanInviteViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface MNDScanInviteViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *scanImgView;

@end

@implementation MNDScanInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.alertView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    [self.scanImgView sd_setImageWithURL:[NSURL URLWithString:self.scanUrl] placeholderImage:nil];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissAction:)];
    [self.view addGestureRecognizer:tapGesture];
    // Do any additional setup after loading the view.
}

- (void)dismissAction:(UITapGestureRecognizer *)gesture {
    [self dismissAlertView];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.7
                          delay:0.0
         usingSpringWithDamping:0.5
          initialSpringVelocity:0.1
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         
                         self.alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
                     }
                     completion:^(BOOL finished) {
                         
                     }];
    
}

- (IBAction)saveScanAction:(UIButton *)sender {
    if (self.scanImgView.image == nil) {
        return;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSError *error;
        UIImage *QRImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.scanUrl] options:NSDataReadingMappedAlways error:&error]];
        if (!error) {
            UIImageWriteToSavedPhotosAlbum(QRImage, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
        } else {
            [NetworkProgressHUD showHudMsg:@"图片保存失败"];
        }
    });
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [NetworkProgressHUD showHudMsg:@"图片保存失败"];
    } else {
        [NetworkProgressHUD showHudMsg:@"图片保存成功"];
        [self dismissAlertView];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"scanImgSaveSuccessNotification" object:nil];
    }
    
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

- (void)dismissAlertView{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
